package com.jiuyue.movehouse.utils;

import cn.hutool.setting.dialect.Props;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author min
 * @description
 * @date 2022/1/8
 */
public class CodeAutoGenerationUtil {

    public static void main(String[] args) {
        CodeAutoGenerationUtil.generation(false, "car");
    }

    public static void generation(Boolean override, String... tableName) {
        // 需要构建一个 代码自动生成器 对象
        AutoGenerator mpg = new AutoGenerator();
        // 配置策略
        // 1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("yourFather");
        gc.setOpen(false);
        gc.setFileOverride(override); // 是否覆盖
        gc.setServiceName("%sService"); // 去Service的I前缀
        gc.setIdType(IdType.AUTO);
        gc.setDateType(DateType.ONLY_DATE);
//        gc.setSwagger2(true);

        mpg.setGlobalConfig(gc);
        //2、设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        DataSourceConfig dataSourceConfig = initDataSource();
        dsc.setUrl(dataSourceConfig.getUrl());
        dsc.setDriverName(dataSourceConfig.getDriverName());
        dsc.setUsername(dataSourceConfig.getUsername());
        dsc.setPassword(dataSourceConfig.getPassword());

        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);
        //3、包的配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName("ranch");
        pc.setParent("com.jiuyue.movehouse");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setController("controller");
        mpg.setPackageInfo(pc);
        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude(tableName);
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setLogicDeleteFieldName("deleted");

        mpg.setStrategy(strategy);
        mpg.execute(); //执行
        System.out.println("生成成功");

    }

    public static DataSourceConfig initDataSource() {
        Props props = new Props("application.yml");
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(props.getStr("url"));
        dataSourceConfig.setDriverName(props.getStr("driver-class-name"));
        dataSourceConfig.setUsername(props.getStr("username"));
        dataSourceConfig.setPassword(props.getStr("password"));
        dataSourceConfig.setDbType(DbType.MYSQL);
        return dataSourceConfig;
    }


}
